<layout name="page" />
<block name="content">
	<input type="hidden" name="ajax" id="ajax" value="1">
	<select name="folder_list" id="folder_list" class="hidden">
		<option value="0">根节点</option>
		{:fill_option($folder_list)}
	</select>
	{:widget('page_header/simple',array('name'=>'后台任务','search'=>'N'))}
	<div class="operate-panel">
		<i class="mid"></i>
		<a class="btn btn-normal" onclick="add();">新增</a>
		<a class="btn btn-normal" onclick="save()">保存</a>
		<i class="split"></i>
		<a class="btn btn-del" onclick="del()">删除</a>
	</div>
	<div class="x1 sub box">
		<div class="w60 m100">
			<div class="table">
				<ul>
					<li class="thead" >
						<span class="w15 x1">名称</span>
						<span class="w15">命令</span>
						<span class="w15">规则</span>
						<span class="w6">状态</span>
					</li>
					<foreach name="list" val="data">
						<li id="{$data.id}">
							<span class="w15 x1">{$data.name}</span>
							<span class="w15">{$data.command}</span>
							<span class="w15">
								<if condition="$data['type']=='D'">
									每{$data.interval}天 {:substr($data['time'],0,5)}执行
								</if>
								<if condition="$data['type']=='S'">
									每{$data.interval}秒执行
								</if></span>
							<span class="w6">{:status($data['is_del'])}</span>
						</li>
					</foreach>
				</ul>
			</div>
		</div>
		<div style="width:16px;"></div>
		<div class="x1 frame" style="height:540px;">
			<form id="form_data" name="form_data" method="post" >
				<input type="hidden" name="id" id="id" >
				<input type="hidden" name="opmode" id="opmode" value="">
				<input type="hidden" name="admin" id="admin">
				<input type="hidden" name="write" id="write">
				<input type="hidden" name="read" id="read">
				<div class="box">
					<label class="input-label" for="name">名称*：</label>
					<input class="input" type="text" id="name" name="name" check="required" info="请输入名称">
				</div>
				<div class="box">
					<label class="input-label" for="command">命令*：</label>
					<input class="input" type="text" id="command" name="command" check="required" info="请输入命令">
				</div>
				<div class="box">
					<label class="input-label">类型*：</label>
					<label class="label w3">每</label>
					<input type="text" class="input w8"  name="interval" id="interval">
					<div style="width:10px;"></div>
					<select class="select w8" name="type" id="type" >
						<option>请选择</option>
						<option value="D">天</option>
						<option value="S">秒</option>
					</select>
					<i class="icon"></i>
					<div style="width:10px;"></div>
					<input class="input hidden w8" type="time" id="time" name="time">
				</div>
				<div class="box">
					<label class="input-label" for="start_date">开始日期*：</label>

					<input class="input input-date" type="text" id="start_date" name="start_date" check="required" info="请选择开始日期">
				</div>
				<div class="box">
					<label class="input-label" for="end_date">结束日期*：</label>

					<input class="input input-date" type="text" id="end_date" name="end_date" check="required" info="">
				</div>
				<div class="box">
					<label class="input-label" for="sort">排序：</label>
					<input class="input w10" type="text" id="sort" name="sort" >
				</div>
				<div class="box">
					<label class="input-label" for="sort">状态：</label>
					<select class="select w10"  name="is_del" id="is_del">
						<option  value="0">启用</option>
						<option value="1">禁用</option>
					</select>
					<i class="icon"></i>
				</div>
				<div class="box">
					<label class="input-label" for="remark" >备注：</label>
					<textarea class="textarea" name="remark" rows="5"></textarea>
				</div>
			</form>
		</div>
	</div>
</block>
<block name="js">
	<script type="text/javascript">
		layui.use('global', function() {
			set_return_url(null);
			var fn = {
				add : function() {
					winopen("{:url('add_timer')}", 560, 470);
				},
				save : function() {
					send_form("form_data", "{:url('save_timer')}", "{:url('timer')}", function(ret) {
						if (ret.status) {
							layer.msg(ret.info, {
								time : 1200
							}, function() {
								location.reload(true);
							});
						}
					});
				},
				del : function() {
					if ($(".table li.active").length == 0) {
						layer.msg('请选择要删除的职位');
						return false;
					}
					//信息框-例2
					layer.msg('确定要删除吗?', {
						time : 0,
						btn : ['确定', '取消'],
						yes : function(index) {
							var vars = $("#form_data").serialize();
							send_ajax("{:url('del')}", vars, function(ret) {
								if (ret.status) {
									layer.msg(ret.info, {
										time : 1200
									}, function() {
										location.reload(true);
									});
								}
							});
						},
						no : function(index) {
							layer.close(index);
						}
					});
				}
			};

			win_exp(fn);

			$(".table li").click(function() {
				$(".table  li.active").removeClass("active");
				$(this).attr("class", "active");
				send_ajax("{:url('read_timer')}", "id=" + $(this).attr("id"), function(data) {
					show_data(data);
				});
				return false;
			});

			function show_data(result) {
				for (var s in result.data) {
					set_val(s, result.data[s]);
					if (s == 'type') {
						if (result.data[s] == 'D') {
							$("#time").removeClass('hidden');
						} else {
							$("#time").addClass('hidden');
						}
					}
				}
				$("#opmode").val("edit");
			};

			$('#btn_del').on('click', function() {
				if ($(".sub-sidebar tr.active").length == 0) {
					layer.msg('请选择要删除的任务');
					return false;
				}
				$('#opmode').val('del');
				//信息框-例2
				layer.msg('确定要删除吗?', {
					time : 0,
					btn : ['确定', '取消'],
					yes : function(index) {
						var vars = $("#form_data").serialize();
						send_ajax("{:url('save_timer')}", vars, function(ret) {
							if (ret.status) {
								layer.msg(ret.info, function() {
									location.reload(true);
								});
							}
						});
					},
					no : function(index) {
						layer.close(index);
					}
				});
			});

			$("#type").on('change', function() {
				if ($("#type").val() == 'D') {
					$("#time").removeClass('hidden');
				} else {
					$("#time").addClass('hidden');
				}
			});
		});

	</script>
</block>